package com.kjlink.internal.report4vue.user;

import cn.xuqiudong.common.base.model.BaseResponse;
import cn.xuqiudong.common.base.model.PageInfo;
import com.kjlink.internal.report4vue.base.controller.BaseApiController;
import com.kjlink.internal.workreport.helper.PasswordHelper;
import com.kjlink.internal.workreport.user.model.User;
import com.kjlink.internal.workreport.user.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 描述:
 *
 * @author Vic.xu
 * @since 2024-12-03 14:06
 */
@RestController
public class ApiUserController extends BaseApiController {

    @Resource
    private UserService userService;

    /**
     * 列表页
     */
    @RequestMapping(value = "/user/list", method = {RequestMethod.GET, RequestMethod.POST})
    public BaseResponse<PageInfo<User>> index(@RequestBody User lookup) {
        PageInfo<User> pager = userService.page(lookup);
        return BaseResponse.success(pager);
    }

    /**
     * 更新启用状态
     *
     * @param id
     * @param enable
     * @return
     */
    @GetMapping("/user/updateStatus/{id}/{enable}")
    private BaseResponse<Integer> updateEnable(@PathVariable Integer id, @PathVariable Boolean enable) {
        User user = new User();
        user.setId(id);
        user.setEnable(enable);
        int update = userService.update(user);
        return BaseResponse.success(update);
    }

    @GetMapping("/user/updateGroup/{id}/{groupId}")
    private BaseResponse<User> updateGroup(@PathVariable Integer id, @PathVariable Integer groupId,
                                           @RequestParam String assignee) {
        User user = new User();
        user.setId(id);
        user.setGroupId(groupId);
        user.setAssignee(assignee);
        userService.update(user);
        user = userService.findById(id);
        return BaseResponse.success(user);
    }

    @PostMapping("/user/save")
    public BaseResponse<?> save(@RequestBody User user) {
        int num = userService.save(user);
        return BaseResponse.success(num);
    }

    /**
     * 更新密码
     */
    @PostMapping("/user/updatePassword")
    public BaseResponse<Boolean> updatePassword(Integer id, String password) {
        User user = new User();
        user.setId(id);
        user.setPassword(PasswordHelper.encryptPassword(password));
        int update = userService.update(user);
        return BaseResponse.success(update == 1);
    }

}
